package com.travel.server.manager.impl.command.project.data;

import com.travel.common.CommonConstant;
import com.travel.common.excel.ExcelData;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.ExcelUtil;
import com.travel.server.manager.dto.request.project.data.ExportProjectDataRequest;
import com.travel.server.manager.dto.request.project.data.QueryProjectDataRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.dto.response.project.data.QueryProjectDataResponse;
import com.travel.server.manager.dto.response.project.data.QueryProjectDataResponse.AnalysisItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/29 12:10
 * @Desc:
 */
@Component
@Slf4j
public class ExportProjectDataCommand extends AbstractCommand<ExportProjectDataRequest, EmptyResponse> {

    @Autowired
    private QueryProjectDataCommand queryProjectDataCommand;

    @Override
    protected void checkInput(ExportProjectDataRequest request) {

    }

    @Override
    public EmptyResponse handle(ExportProjectDataRequest request) {
        QueryProjectDataRequest queryProjectDataRequest = new QueryProjectDataRequest();
        queryProjectDataRequest.setGroup(request.getGroup());
        queryProjectDataRequest.setDataStartTime(request.getStartTime());
        queryProjectDataRequest.setDataEndTime(request.getEndTime());
        queryProjectDataRequest.setStartTime(request.getStartTime());
        queryProjectDataRequest.setEndTime(request.getEndTime());
        queryProjectDataRequest.setProject(request.getProject());
        QueryProjectDataResponse response = queryProjectDataCommand.handle(queryProjectDataRequest);
        List<AnalysisItem> list = response.getAnalysisList();
        if(CollectionUtil.isEmpty(list)){
            throw new WorkException(WorkCode.EXCEL_DATA_NULL);
        }
        List<String> title = Arrays.asList("项目信息", "曝光次数", "访客数", "浏览量", "申请人数", "访问-申请转化率", "成单数", "申请-成单转化率", "咨询量");
        List<List<Object>> rows = new ArrayList<>();
        for(AnalysisItem item : list) {
            List<Object> objectList = new ArrayList<>();
            objectList.add(item.getProject());
            objectList.add(item.getExposure());
            objectList.add(item.getVisit());
            objectList.add(item.getBrowse());
            objectList.add(item.getApply());
            objectList.add(item.getVisitApply());
            objectList.add(item.getOrder());
            objectList.add(item.getApplyOrder());
            objectList.add(item.getConsul() == null ? CommonConstant.EMPTY_STR : item.getConsul());
            rows.add(objectList);
        }
        ExcelData excelData = new ExcelData();
        excelData.setName("项目分析");
        excelData.setRows(rows);
        excelData.setTitles(title);
        ExcelUtil.exportExcel(getHttpServletResponse(), "项目分析报表", excelData);
        return new EmptyResponse();
    }
}
